<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/7/27
 * Time: 10:35
 */
namespace Mengwu\Controller;

use Mengwu\Api\MwApi;

class ZengwuxianController extends BaseStoreController{

    public function index(){

        $this->assignTitle('赠无限');
        //查找类目

        $data = [];
        $result = mwApi(MwApi::BY_Product_giftCate,$data);
        if($result['status']){
            $this->assign('cate',$result['info']);
        }else{
            $this->errorPage('数据读取失败');
        }

        //查找商品列表
        $zone_id = I('zone_id');
        $order = I('order',19);
        $cate_id = I('cate');
        $page_no = I('p',1);
        $page_size = 9;
        $data = [
            'zone_id' => $zone_id,
            'order' => $order,
            'cate_id' => $cate_id,
            'page_no' => $page_no,
            'page_size' => $page_size
        ];

        $result = mwApi(MwApi::BY_Product_listGift,$data);
        if($result['status']){
            $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
            if($result['info']['count']>0){
                $show = paging($page_no,$page_size,$result['info']['count'],'mengwu/zengwuxian/index');
                $this->assign('show',$show);

                $list = $result['info']['list'];
                foreach($list as &$val){
                    $val['head'] = getHeadUrl($val['head'],60);
                    $val['main_img'] = $PICTURE_REMOTE_URL.$val['main_img'].'&size=400';
                    $val['date'] = $this->getTimeInterval($val['createtime']);
                }

                $this->assign('list',$list);
            }
        }else{
            $this->errorPage('数据读取失败');
        }

        $this->boye_display();
    }

    /**
     * 赠无限详情
     */
    /*public function detail(){
        $this->boye_display();
    }*/

    /**
     * 获取时间间隔
     * @param $time
     */
    private function getTimeInterval($time){
        $now = time();
        $interval = $now -$time;
        if($interval < 60){
            $show = $interval.'秒前';
        }
        elseif($interval < 3600){
            $show = floor($interval/60).'分钟前';
        }
        elseif($interval < 3600*24){
            $show = floor($interval/3600).'小时前';
        }
        elseif($interval < 3600*24*7){
            $show = floor($interval/3600/24).'天前';
        }
        elseif($interval < 3600*24*30){
            $show = floor($interval/3600/24/7).'周前';
        }
        elseif($interval < 3600*24*365){
            $show = floor($interval/3600/24/365).'月前';
        }
        else{
            $show = date('Y-m-d H:i:s',$time);
        }
        return $show;
    }

}